package proto_wearebatman;

public class Composite extends Circuit implements IhasInput, IhasOutput {

    private Input[] inputs;
    private Output[] outputs;
    private Wire[] wireInputs;
    private Wire[] wireOutputs;
    public int counter;
    protected int prevInput0, prevInput1;

    public Composite(String name, String filename, int in, int out) {
        super(filename);
        
        this.outputs = new Output[out];
        for (int i = 0; i < out; i++) {
            this.outputs[i] = new Output();
        }
        this.inputs = new Input[in];
        for (int i = 0; i < in; i++) {
            this.inputs[i] = new Input();
        }
        this.wireOutputs = new Wire[out];
        this.wireInputs = new Wire[in];
        this.name = name;
        
        this.prevInput0 = 0;
        this.prevInput1 = 0;
        this.startedSimulation = true;

        // előző értékek miatt kell szinkronizálni
        this.tick(1);
        this.counter = 0;

        // Proto Output
        Main.println("Composite created: " + this.name);
    }

    public void setInputData(int pin, int data) {
        this.inputs[pin].setData(data);
    }

    public int getInputData(int pin) {
        return this.inputs[pin].getData();
    }

    public void connectWireInput(int pin, Wire wire) {
        this.wireInputs[pin] = wire;
    }

    public void operate() {
        for(int i=0; i<this.inputs.length; i++) {
            String sw = table.get("in"+(i+1)).toString();
            try {
                ((Switch)this.sourcelist.get(Integer.parseInt(sw.substring(1).toString()))).setStateSilent(this.getInputData(i));
            }
            catch(Exception e) {
                Main.println("ErrorInComposite: "+this.name);
            }
        }
        tick(2);
        for(int i=0; i<this.outputs.length; i++) {
            String sw = table.get("out"+(i+1)).toString();
            try {
                this.outputs[i].setData(Integer.parseInt(((Led)this.gatelist.get(Integer.parseInt(sw.substring(1).toString()))).getFinalState()));
            }
            catch(Exception e) {
                Main.println("ErrorInComposite: "+this.name);
            }
        }        

        for(int i=0; i<this.outputs.length; i++)
            this.wireOutputs[i].operate();

        /*if((prevInput0 != this.inputs[0].getData() || prevInput1 != this.inputs[1].getData()) && counter > (this.inputs.length-1)) {
            Main.println("Race Condition in "+this.name+"!");
            return;
        }
        this.prevInput0 = this.inputs[0].getData();
        this.prevInput1 = this.inputs[1].getData();
        //Main.println(this.name + ".operate() | Data = " + this.outputs[0].getData());
        this.counter++;
        String s = table.get("in1").toString();
        IhasInput it = (IhasInput)this.gatelist.get(Integer.parseInt(s.substring(1).toString()));
        it.setInputData(0, this.inputs[0].getData());
        s = table.get("in2").toString();
        it = (IhasInput)this.gatelist.get(Integer.parseInt(s.substring(1).toString()));
        it.setInputData(0, this.inputs[1].getData());
        this.tick(1);
        s = table.get("out1").toString();
        Ioperable item = this.gatelist.get(Integer.parseInt(s.substring(1).toString()));
        this.outputs[0].setData(Integer.parseInt(item.getFinalState()));
        if(counter < 10)
            this.wireOutputs[0].operate();*/
        
    }

    public int getOutputData(int pin) {
        return this.outputs[pin].getData();
    }

    public void setOutputData(int pin, int data) {
        this.outputs[pin].setData(data);
    }

    public void connectWireOutput(int pin, Wire wire) {
        this.wireOutputs[pin] = wire;
    }

    public String getName() {
        return this.name;
    }

    public void resetCounter() {

    }

    public String getFinalState() {
        return Integer.toString(this.outputs[0].getData());
    }
}
